import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Solution1305 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
        List<Integer> list=new ArrayList<>();
        dfs(root1,list);
        dfs(root2,list);
        list.sort(Comparator.comparingInt(a -> a));
        return new ArrayList<>(list);
    }

    private void dfs(TreeNode root, List<Integer> list) {
        if (root==null){
            return;
        }
        list.add(root.val);
        dfs(root.left,list);
        dfs(root.right,list);
    }

    public static void main(String[] args) {
        System.out.println(new Solution1305().getAllElements(new TreeNode(2,new TreeNode(1),new TreeNode(4)),new TreeNode(1,new TreeNode(0),new TreeNode(3))));
    }
}
